﻿using Kanban.Api.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kanban.Business;
using Kanban.Entity;

namespace Kanban.Api.Controllers
{
    [Authorize(Roles="Master")]
    public class BoardController : Controller
    {
        
        public ActionResult Index()
        {
            return View();
        }

        [NoCache]
        public ActionResult Details(string key)
        {
            var business = new QuadroBusiness();
            Quadro quadro = business.ObterPorChave(key);

            if (quadro == null)
                throw new HttpException(404, "Board not found.");

            ViewBag.Quadro = quadro;

            return View();
        }

        [NoCache]
        [HttpGet]
        public JsonResult GetTasks(string key)
        {
            var business = new TarefaBusiness();
            List<Tarefa> tarefas = business.ListarPorChaveQuadro(key);

            if (tarefas == null)
                tarefas = new List<Tarefa>();

            return Json(tarefas, JsonRequestBehavior.AllowGet);
        }

        [NoCache]
        [HttpPut]
        public JsonResult UpdateTask(string key, Tarefa tarefa)
        {
            var tarefaBusiness = new TarefaBusiness();

            if (tarefaBusiness.Obter(tarefa.Id, key) == null)
                throw new HttpException(404, "Task not found.");

            tarefa.Responsavel = new Usuario();
            tarefa.Responsavel.Id = 0;

            tarefaBusiness.Atualizar(tarefa);

            return Json(tarefaBusiness.ObterPorId(tarefa.Id));
        }        

        [NoCache]
        [HttpPost]
        public JsonResult AddTask(string key, Tarefa tarefa)
        {
            var quadroBusiness = new QuadroBusiness();
            var tarefaBusiness = new TarefaBusiness();

            Quadro quadro = quadroBusiness.ObterPorChave(key);

            if(quadro == null)
                throw new HttpException(404, "Board not found.");

            tarefa.Quadro = quadro;
            tarefa.CriadaPor = new Usuario();
            tarefa.CriadaPor.Id = 1;
            tarefa.Status = 0;

            tarefaBusiness.Adicionar(tarefa);

            return Json(tarefaBusiness.ObterPorId(tarefa.Id));
            
        }

        
        [NoCache]
        [HttpDelete]
        public JsonResult DeleteTask(string key, int taskId)
        {
            var tarefaBusiness = new TarefaBusiness();

            var tarefa = tarefaBusiness.Obter(taskId, key);

            if (tarefa == null)
                throw new HttpException(404, "Task not found.");     

            tarefaBusiness.Remover(taskId);

            tarefa.Excluido = true;

            return Json(tarefa);
        }

        [NoCache]
        [HttpPost]        
        public string Create(BoardViewModel board)
        {

            var business = new QuadroBusiness();
            var quadro = new Quadro();
            quadro.Nome = board.Name;
            quadro.Descricao = string.Empty;
            quadro.Colunas = new List<QuadroColuna>();
            quadro.CriadoPor = new Usuario();
            quadro.CriadoPor.Id = 1;
            quadro.Equipe = new Equipe();

            foreach (string coluna in board.Columns)
            {
                var item = new QuadroColuna();
                item.Nome = coluna;
                item.Ordem = quadro.Colunas.Count + 1;

                quadro.Colunas.Add(item);
            }

            business.Adicionar(quadro);

            quadro = business.ObterPorId(quadro.Id);

            return "\"" + quadro.Chave + "\"";
        }
                
    }
}
